package com.probox.common.entity.service.common.impl;

import com.probox.common.core.constant.CachePrefix;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.mapper.common.RegionMapper;
import com.probox.common.entity.vo.regin.SysProvinceVo;
import com.probox.common.redis.service.RedisService;
import com.probox.common.entity.service.common.SysReginService;
import com.probox.common.entity.vo.regin.SysProvince;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SysReginServiceImpl implements SysReginService {
    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private RedisService redisService;

    @Override
    public List<SysProvince> getRegion() {
        redisService.deleteObject(CachePrefix.PLATFORM_AREA);
        List<SysProvince> cacheList = redisService.getCacheList(CachePrefix.PLATFORM_AREA);
        List<SysProvince> sysProvinceList = cacheList;
        if (sysProvinceList == null || sysProvinceList.size() == 0) {
            sysProvinceList = regionMapper.getSysProvinceList();
            redisService.setCacheList(CachePrefix.PLATFORM_AREA, sysProvinceList);
        }
        return sysProvinceList;
    }

    @Override
    public List<SysProvinceVo> getRegionS() {
//        redisService.deleteObject(CachePrefix.PLATFORM_AREA);
//        List<SysProvinceVo> cacheList = redisService.getCacheList(CachePrefix.PLATFORM_AREA);
//        = null;
//        if (sysProvinceList == null || sysProvinceList.size() == 0) {
        List<SysProvinceVo>   sysProvinceList = regionMapper.getSysProvinceListS();
//            redisService.setCacheList(CachePrefix.PLATFORM_AREA, sysProvinceList);
//        }
        return sysProvinceList;
    }

    @Override
    public Long[] getReginCode(String[] labels) {
        if (labels == null){
            return null;
        }
        String provinceName = labels[0];
        String cityName = labels[1];
        String areaName = null;
        SysProvince reginCode = null;
        if (labels.length == 3 && StringUtils.isNotEmpty(cityName)){
            areaName = labels[2];
            reginCode = regionMapper.getReginCode(provinceName, cityName, areaName);
        }else if (StringUtils.isNotEmpty(cityName)) {
            reginCode = regionMapper.getReginPCCode(provinceName, cityName);
        }
        if (reginCode!=null){
            Long provinceCode = reginCode.getValue();
            Long cityCode = reginCode.getChildren().get(0).getValue();
            Long areaCode = null;
            if (reginCode.getChildren().get(0).getChildren()!=null){
                areaCode =  reginCode.getChildren().get(0).getChildren().get(0).getValue();
            }else {
                areaCode = null;
            }
            Long[] regins = new Long[3];
            regins[0] = provinceCode;
            regins[1] = cityCode;
            if (areaCode != null){
                regins[2] = areaCode;
            }
            return regins;
        }else {
            return new Long[0];
        }

    }
}
